//
//  CanvasView.swift
//  SpinCut
//
//  Created by dai simon on 2024/7/6.
//

import UIKit

class CanvasView: UIView {
    var angle: CGFloat = 0
    var speed: CGFloat = 0.5 {
        didSet {
            if speed == 0 {
                stopRotate()
            } else {
                startRotate()
            }
        }
    }
    
    var displayLink: CADisplayLink?
    let image = UIImage(named: "pattern")?.convertImageToBlack()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        startRotate()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.backgroundColor = .clear
        startRotate()
    }
    
    deinit {
       stopRotate()
   }

    func stopRotate() {
        displayLink?.invalidate()
        displayLink = nil
    }

    func startRotate() {
        guard displayLink == nil else {
            return
        }
        displayLink = CADisplayLink(target: self, selector: #selector(updateRotation))
        displayLink?.add(to: .main, forMode: .default)
    }
    
    @objc private func updateRotation() {
        angle += speed * 3 * CGFloat.pi / 180 // 每次更新旋转的角度
        self.transform = CGAffineTransform(rotationAngle: angle)
    }
    
    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }

        let lineWidth: CGFloat = 2
        let space: CGFloat = 6
        
        // 绘制内部实心白色圆
        let center = CGPoint(x: rect.width / 2, y: rect.height / 2)
        let radius = min(rect.width, rect.height) / 2 - space
        let innerCirclePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
        
        context.addPath(innerCirclePath.cgPath)
        context.setFillColor(UIColor.white.cgColor)
        context.fillPath()

        // 设置虚线的样式
        context.setLineWidth(lineWidth)
        context.setStrokeColor(UIColor.white.cgColor)
        let dashPattern: [CGFloat] = [lineWidth, 8]
        context.setLineDash(phase: 0, lengths: dashPattern)

        // 绘制圆形虚线路径
        let outerRadius = radius + space
        let circlePath = UIBezierPath(arcCenter: center, radius: outerRadius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)

        context.addPath(circlePath.cgPath)
        context.strokePath()
        
        guard let image = image else { return }
                
        let imageRect = CGRect(x: 100,
                               y: 100,
                               width: image.size.width / 2,
                               height: image.size.height / 2)
        image.draw(in: imageRect)
    }
    
}

extension UIImage {
    func convertImageToBlack() -> UIImage? {
        let rect = CGRect(origin: .zero, size: self.size)
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let context = UIGraphicsGetCurrentContext()
        context?.translateBy(x: 0, y: self.size.height)
        context?.scaleBy(x: 1.0, y: -1.0)

        context?.setBlendMode(.normal)
        context?.draw(self.cgImage!, in: rect)

        context?.setBlendMode(.sourceIn)
        context?.setFillColor(UIColor.black.cgColor)
        context?.fill(rect)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }
}
